﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qu.ComputingContest.Model
{
    public class ScoreRepository
    {
        public static bool  IsFinalRound() {
            var ccDB = new ComputingContestDB();
            return ccDB.Database.SqlQuery<bool>("Select dbo.[IsFinalRound]()").FirstOrDefault();
        }

        public static int ScoreboardRefreshTimeSpan()
        {
            var ccDB = new ComputingContestDB();
            return ccDB.Database.SqlQuery<int>("Select [dbo].[GetConfigValueAsInt] ('ScoreboardRefreshTimeSpan')").FirstOrDefault();
        }
        public static int NumWinnersOnScoreboard()
        {
            var ccDB = new ComputingContestDB();
            return ccDB.Database.SqlQuery<int>("Select [dbo].[GetConfigValueAsInt] ('NumWinnersOnScoreboard')").FirstOrDefault();
        }

        public static int? GetTeamNo(int registrationID)
        {
            var ccDB = new ComputingContestDB();
            var rego = ccDB.Registrations.Where(r=> r.RegistrationID == registrationID).FirstOrDefault();
            if (rego != null)
                return rego.TeamNo;
            else
                return null;
        }

        public static void SetStartFinalRound(string finalRoundDateTime)
        {
            var ccDB = new ComputingContestDB();
            var config = ccDB.Configs.Where(c => c.KeyName == "ContestFinalRoundTime").FirstOrDefault();
            config.KeyValue = finalRoundDateTime;  
            ccDB.SaveChanges();
        }
    }
}
